package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.constant.Result;
import com.itheima.domain.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/10/14 20:42
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    //分页查询;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.findPage(queryPageBean);
        return pageResult;
    }

    //查询全部;
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, "查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //添加;
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        try {
            menuService.add(menu);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    //删除;
    @RequestMapping("/deleteByid")
    public Result deleteByid(Integer id) {
        try {
            menuService.deleteByid(id);
            return new Result(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    //根据ID查询;
    @RequestMapping("/findByid")
    public Result findByid(Integer id) {
        try {
            Menu menu = menuService.findByid(id);
            return new Result(true, "查询成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //修改数据;
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu) {
        try {
            menuService.update(menu);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }
    //查询展示菜单列表
    @RequestMapping("/getMenu")
    public Result getMenu(Principal principal){
        try {
            String name = principal.getName();
            List<Menu> list = menuService.getMenu(name);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }
}
